package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Member;
import cn.itcast.service.MemberFilesService;
import com.alibaba.dubbo.config.annotation.Reference;
//import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

//会员业务控制器

@RestController
@RequestMapping("/member")
@CrossOrigin
public class MemberFilesController {

    @Reference
    private MemberFilesService memberFilesService;



    /**
     * 新增会员项
     *
     * @param member
     * @return
     */
    @PostMapping("/add")
    //@PreAuthorize("hasAuthority('USER_ADD')")
    //@RequestBody 接收前端传递给后端的json字符串中的数据的(请求体中的数据的)
    public Result add(@RequestBody Member member) {
        try {
            memberFilesService.add(member);
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
    }

    /**
     * 分页查询会员项数据
     * 有条件档案，姓名，手机
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping(path = "/findPage")
    //@PreAuthorize("hasAuthority('USER_QUERY')")
    public Result findPage(QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = memberFilesService.pageQuery(queryPageBean);
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_MEMBER_FAIL);
        }
    }


    /**
     * 在修改的时候
     * 根据id查询数据详情
     *
     * @param id
     * @return
     */
    @GetMapping(path = "/{id}")
    // @PreAuthorize("hasAuthority('USER_QUERY')")
    public Result findById(@PathVariable int id) {
        try {
            //查询会员项
            Member member = memberFilesService.findById(id);
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS, member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MEMBER_FAIL);
        }
    }

    /**
     * 确定编辑, 更新数据
     *
     * @param member
     * @return
     */
    @PutMapping(path = "/update")
    //@PreAuthorize("hasAuthority('USER_EDIT')")
    public Result edit(@RequestBody Member member) {
        try {
            memberFilesService.edit(member);
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
        }
    }


    /**
     * 修改会员状态
     *
     * @param
     * @return
     */
    @PutMapping("/{id}/{status}")
    //@PreAuthorize("hasAuthority('USER_EDIT')")

    public Result editStstus(@PathVariable("id") Integer id, @PathVariable("status") Integer status) {
        try {
            memberFilesService.editStstus(id, status);
            return new Result(true, MessageConstant.UPDATE_MEMBER_STATUS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_MEMBER_STATUS_FAIL);
        }
    }

}


